<?php

class Ctrl_user extends CI_Controller {

	function __construct() {
		parent::__construct();
		if (!$this->session->userdata('adminin')) {
			redirect(base_url().'admin/home');
		}
		$this->load->model('Model_user');
	}

	function index() {
		//menampilkan list user
		$this->list_user();
	}

	function list_user() {
		$list = $this->Model_user->select_all();
		if ($list != FALSE) {
			$this->load->library('table');
			$this->table->set_template(array('table_open' => '<table align="center" id="tabel_user" width="100%" cellpadding="4" cellspacing="3">'));
			$this->table->set_heading('No', 'Nama', 'Username', 'Role', 'Aksi');
			$i = 1;
			foreach ($list as $entry) {
				$this->table->add_row(
						$i, 
						$entry->nama, 
						$entry->username, 
						$entry->role, 
						'<span class="anchor">
							<a href="' . base_url() . 'admin/ctrl_user/edit/' . $entry->username . '">Edit</a>
						</span> | 
						<span class="anchor">
							<a onclick="return confirm_message(\'Apakah anda yakin akan menghapus?\')" href="' . base_url() . 'admin/ctrl_user/delete/' . $entry->username . '">Hapus</a>
						</span>'
				);
				$i++;
			}
			$display['content'] = $this->table->generate();
		}else{
			$display['content'] = 'Belum ada user';
		}
		$data['title'] = 'Daftar User';
		$this->load->view('admin/admin_header_view', $data);
		$this->load->view('admin/list_user', $display);
		$this->load->view('admin/admin_footer_view');
	}

	function add_user() {
		$data['title'] = 'Tambah User';
		$this->load->view('admin/admin_header_view', $data);
		$this->load->view('admin/add_user');
		$this->load->view('admin/admin_footer_view');
	}

	function insert() {
		$_POST['password'] = md5($_POST['password']);
		$this->Model_user->insert($_POST);
		redirect(base_url() . 'admin/ctrl_user');
	}

	function delete($usr) {
		$this->Model_user->delete($usr);
		redirect(base_url() . 'admin/ctrl_user');
	}

	function edit($usr) {
		$display['content'] = $this->Model_user->select_with_username($usr);
		$data['title'] = 'Daftar User';
		$this->load->view('admin/admin_header_view', $data);
		$this->load->view('admin/edit_user', $display);
		$this->load->view('admin/admin_footer_view');
	}

	function update() {
		$usr = $this->input->post('username');
		$this->Model_user->update($usr, $_POST);
		redirect(base_url() . 'admin/ctrl_user');
	}
}